package com.jobs.common.config;

import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.ToStringSerializer;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.http.MediaType;
import org.springframework.http.converter.FormHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.servlet.config.annotation.*;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

/**
 * 基础入口配置
 */
@Configuration
@EnableWebMvc
public class WebMvcConfig implements WebMvcConfigurer {

    private final Charset charset = Charset.forName("UTF-8");

    @Autowired
    private Environment environment=null;

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**").
                allowedOriginPatterns("*").
                allowedMethods("POST", "GET", "OPTIONS").
                maxAge(3600).
                allowCredentials(true);
    }

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        StringHttpMessageConverter converter = new StringHttpMessageConverter(charset);
        FormHttpMessageConverter converter1 = new FormHttpMessageConverter();
        converter1.setCharset(charset);

        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
        // 添加fastjson的配置信息 比如 ：是否要格式化返回的json数据
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        // 这里就是核心代码了，WriteMapNullValue把空的值的key也返回
        fastJsonConfig.setSerializerFeatures(SerializerFeature.WriteMapNullValue);
        SerializeConfig.globalInstance.put(Long.class, ToStringSerializer.instance);
        SerializeConfig serializeConfig = SerializeConfig.globalInstance;
       // serializeConfig.put(LocalDateTimeDateConverter.class, LocalDateTimeSerializer.INSTANCE);
        fastJsonConfig.setSerializeConfig(serializeConfig);
        fastConverter.setFastJsonConfig(fastJsonConfig);
        MediaType mediaType =  new MediaType(MediaType.APPLICATION_JSON_UTF8_VALUE);
        List<MediaType> fastMediaTypes = new ArrayList<>();
        fastMediaTypes.add(mediaType);
        fastJsonConfig.setCharset(charset);
        fastConverter.setSupportedMediaTypes(fastMediaTypes);
        fastConverter.setDefaultCharset(charset);
        converters.add(converter);
        converters.add(converter1);
        converters.add(fastConverter);
    }
    
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry){
        //模型结果文件路径
        String outputDir=environment.getProperty("static.out");
        if(outputDir!=null){
            String[] outputDirList=outputDir.split(",");
            for(int i=0;i<outputDirList.length;i++){
                outputDirList[i]="file:///"+outputDirList[i];
            }
            registry.addResourceHandler("/productPic/**")
                    .addResourceLocations(outputDirList);
        }
    }

}
